package com.superman.service.socket;

import com.alibaba.fastjson.JSON;
import com.superman.entity.Car;
import com.superman.entity.Car_Brand_Category;
import com.superman.entity.User;
import com.superman.service.IAddAndAlter;
import com.superman.service.ILoginAndRegister;
import com.superman.service.IQuery;
import com.superman.service.IRentAndReturn;
import com.superman.service.impl.AddAndAlterImpl;
import com.superman.service.impl.LoginAndRegisterImpl;
import com.superman.service.impl.QueryImpl;
import com.superman.service.impl.RentAndReturnImpl;

import java.util.List;

public class AllService {
    ILoginAndRegister loginAndRegister = new LoginAndRegisterImpl();
    IQuery query = new QueryImpl();
    IRentAndReturn rentAndReturn = new RentAndReturnImpl();
    IAddAndAlter addAndAlter = new AddAndAlterImpl();

    /**
     * 请求处理方法解析器
     * @param request 客户端发出的请求
     * @return 服务端的响应
     */
    public String getResponse(String request){
        String[] ask = request.split("#");
        String response = null;
        switch (ask[0]){
            case "login":
                User user1 = JSON.parseObject(ask[1], User.class);
                response = JSON.toJSONString(loginAndRegister.login(user1));
                break;

            case "register":
                User user2 = JSON.parseObject(ask[1], User.class);
                response = loginAndRegister.register(user2);
                break;

            case "userQueryCar":
                response = JSON.toJSONString(query.userQueryCar());
                break;

            case "adminQueryCar":
                response = JSON.toJSONString(query.adminQueryCar());
                break;

            case "userQueryRecord":
                response = JSON.toJSONString(query.userQueryRecord(ask[1]));
                break;

            case "adminQueryRecord":
                response = JSON.toJSONString(query.adminQueryRecord());
                break;

            case "rentCar"://注意：此处的两个参数需要分别修改为user对象和car对象
                response = JSON.toJSONString(rentAndReturn.rentCar(ask[1], ask[2]));
                break;

            case "queryRecordByUserIdAndNoReturnInfo"://查询指定用户尚未归还的租车记录信息
                response = JSON.toJSONString(query.queryRecordByUserIdAndNoReturnInfo(ask[1]));
                break;

            case "returnCar":
                response = JSON.toJSONString(rentAndReturn.returnCar(ask[1]));
                break;

            case "queryBrand":
                response = JSON.toJSONString(query.queryBrand());
                break;

            case "queryCategory":
                response = JSON.toJSONString(query.queryCategory());
                break;

            case "addCar":
                Car car = JSON.parseObject(ask[1], Car.class);
                response = JSON.toJSONString(addAndAlter.addCar(car));
                break;

            case "alterCarRent":
                response = JSON.toJSONString(addAndAlter.alterCarRent(ask[1], Double.valueOf(ask[2])));
                break;
            case "alterCarUseable":
                response = JSON.toJSONString(addAndAlter.alterCarUseable(ask[1], Integer.valueOf(ask[2])));
                break;
        }
        return response;
    }
}
